package org.apache.ibatis.common.reflection;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

/**
 * @Name DefaultReflectorFactory
 * @Description: 反射器工厂 [实现] 类
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-25 17:27:23
 **/
public class DefaultReflectorFactory implements ReflectorFactory {
    private boolean classCacheEnabled = true;
    private final Map<Class<?>, Reflector> cache = new ConcurrentHashMap<>();

    @Override
    public boolean isClassCacheEnbaled() {
        return classCacheEnabled;
    }

    @Override
    public void setClassCacheEnabled(boolean classCacheEnabled) {
        this.classCacheEnabled = classCacheEnabled;
    }

    @Override
    public Reflector findForClass(Class<?> type) {
        if (classCacheEnabled) {
            return cache.computeIfAbsent(type, Reflector::new);
        } else {
            return new Reflector(type);
        }
    }
}

